<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="utf-8">
  <title>Dashboard &laquo; Admin</title>
  <link rel="stylesheet" href="../assets/vendors/bootstrap/css/bootstrap.css">
  <link rel="stylesheet" href="../assets/vendors/font-awesome/css/font-awesome.css">
  <link rel="stylesheet" href="../assets/vendors/nprogress/nprogress.css">
  <link rel="stylesheet" href="../assets/css/admin.css">
  <script src="../assets/vendors/nprogress/nprogress.js"></script>
  <script src="/login/status"></script>
  <script>
    if (!isLogin) location.href = 'login.html';
  </script>
</head>

<body>
  <script>
    NProgress.start()
  </script>

  <div class="main">
    <nav class="navbar">
      <button class="btn btn-default navbar-btn fa fa-bars"></button>
      <ul class="nav navbar-nav navbar-right">
        <li><a href="profile.html"><i class="fa fa-user"></i>个人中心</a></li>
        <li><a href="javascript:;" id="logout"><i class="fa fa-sign-out"></i>退出</a></li>
      </ul>
    </nav>
    <div class="container-fluid">
      <div class="page-title">
        <h1>我的个人资料</h1>
      </div>
      <!-- 有错误信息时展示 -->
      <!-- <div class="alert alert-danger">
        <strong>错误！</strong>发生XXX错误
      </div> -->
      <div id="forms"></div>
      <form class="form-horizontal">
        <div class="form-group">
          <label class="col-sm-3 control-label">头像</label>
          <div class="col-sm-6">
            <label class="form-image">
              <input id="avatar" type="file">
              <img id="avatarimg" src='/assets/img/default.png'>
              <input type="hidden" name="avatar" id="hiddenavatar">
              <i class="mask fa fa-upload"></i>
            </label>
          </div>
        </div>
        <div class="form-group">
          <label for="email" class="col-sm-3 control-label">邮箱</label>
          <div class="col-sm-6">
            <input id="email" class="form-control" name="email" type="type" value="coder@itcast.cn" placeholder="邮箱"
              readonly>
            <p class="help-block">登录邮箱不允许修改</p>
          </div>
        </div>
        <div class="form-group">
          <label for="nickname" class="col-sm-3 control-label">昵称</label>
          <div class="col-sm-6">
            <input id="nickname" class="form-control" name="nickName" type="type" value="张无忌" placeholder="昵称">
            <p class="help-block">限制在 2-16 个字符</p>
          </div>
        </div>
        <div class="form-group">
          <div class="col-sm-offset-3 col-sm-6">
            <button type="submit" class="btn btn-primary">更新</button>
            <a class="btn btn-link" href="password-reset.html">修改密码</a>
          </div>
        </div>
      </form>
    </div>
  </div>

  <div class="aside">
    <div class="profile">
      <img class="avatar" src="../uploads/avatar.jpg">
      <h3 class="name">Coder</h3>
    </div>
    <ul class="nav">
      <li>
        <a href="index.html"><i class="fa fa-dashboard"></i>仪表盘</a>
      </li>
      <li>
        <a href="#menu-posts" class="collapsed" data-toggle="collapse">
          <i class="fa fa-thumb-tack"></i>文章<i class="fa fa-angle-right"></i>
        </a>
        <ul id="menu-posts" class="collapse">
          <li><a href="posts.html">所有文章</a></li>
          <li><a href="post-add.html">写文章</a></li>
          <li><a href="categories.html">分类目录</a></li>
        </ul>
      </li>
      <li>
        <a href="comments.html"><i class="fa fa-comments"></i>评论</a>
      </li>
      <li>
        <a href="users.html"><i class="fa fa-users"></i>用户</a>
      </li>
      <li>
        <a href="#menu-settings" class="collapsed" data-toggle="collapse">
          <i class="fa fa-cogs"></i>设置<i class="fa fa-angle-right"></i>
        </a>
        <ul id="menu-settings" class="collapse">
          <li><a href="slides.html">图片轮播</a></li>
          <li><a href="settings.html">网站设置</a></li>
        </ul>
      </li>
    </ul>
  </div>

  <script src="../assets/vendors/jquery/jquery.js"></script>
  <script src="../assets/vendors/bootstrap/js/bootstrap.js"></script>
  <script src="/assets/js/common.js"></script>
  <script src="/assets/vendors/art-template/template-web.js"></script>
  <script>
    NProgress.done()
  </script>
</body>
<script type="text/html" id="personalData">
  {{ each data v }}
  <form class="form-horizontal">
    <div class="form-group">
      <label class="col-sm-3 control-label">头像</label>
      <div class="col-sm-6">
        <label class="form-image">
          <input id="avatar" type="file" value="{{ v._id }}">
          <img src="{{ v.avatar || '/assets/img/default.png' }}">
          <i class="mask fa fa-upload"></i>
        </label>
      </div>
    </div>
    <div class="form-group">
      <label for="email" class="col-sm-3 control-label">邮箱</label>
      <div class="col-sm-6">
        <input id="email" class="form-control" name="email" type="type" value="coder@itcast.cn" placeholder="邮箱"
          readonly>
        <p class="help-block">登录邮箱不允许修改</p>
      </div>
    </div>
    <div class="form-group">
      <label for="nickname" class="col-sm-3 control-label">昵称</label>
      <div class="col-sm-6">
        <input id="nickname" class="form-control" name="nickname" type="type" value="张无忌" placeholder="昵称">
        <p class="help-block">限制在 2-16 个字符</p>
      </div>
    </div>
    <div class="form-group">
      <div class="col-sm-offset-3 col-sm-6">
        <button type="submit" class="btn btn-primary">更新</button>
        <a class="btn btn-link" href="password-reset.html">修改密码</a>
      </div>
    </div>
  </form>
  {{ /each }}
</script>
<script>
  $(() => {
    var profileArr = []
    var id = sessionStorage.curUser

    $('.form-horizontal').on('submit', function () {
      var formData = $('.form-horizontal').serialize()
      console.log(formData)

      $.ajax({
        url: '/users/' + id,
        type: 'PUT',
        data: formData,
        success: (newres) => {
          console.log(newres);

        }
      })

      return false

    })
    $.ajax({
      type: "GET",
      url: "/users/" + id,
      success: function (res) {
        console.log(res);
        $('#avatarimg').attr('src', res.avatar)
        $('#email').val(res.email)
        $('#nickname').val(res.nickName)
      }
    }) //获取当前用户的信息  

    $('#avatar').on('change', function () {
      var avatarData = new FormData();
      // console.log(this.files[0]);
      avatarData.append('avatar', this.files[0]);
      $.ajax({
        type: 'post',
        url: '/upload',
        data: avatarData,
        processData: false,
        contentType: false,
        success: (res) => {
          $('#avatarimg').attr('src', res[0].avatar)
          $('#hiddenavatar').val(res[0].avatar)
        },
        error: () => {
          console.log('添加失败');
        }
      })
    })

  })
</script>

</html>
